package com.yiwei.jmx.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.api.model.DealIndexDetailModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class BorrowUseAdapter extends ItemAdapter<DealIndexDetailModel.DataBean.UseType, BorrowUseAdapter.DefaultHolder> {

    private int selectPoi = -1;

    public BorrowUseAdapter(Context context) {
        super(context);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_borrow_use, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        DealIndexDetailModel.DataBean.UseType item = getItem(position);
        holder.txtName.setText(item.name);

        if(selectPoi==position){
            holder.cbCheck.setImageResource(R.mipmap.img_check_select);
        }else {
            holder.cbCheck.setImageResource(R.mipmap.img_check_unselect);
        }
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onSuccess(item.id, item.name);
                }
                selectPoi = position;
                notifyDataSetChanged();
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.cb_check)
        ImageView cbCheck;
        @BindView(R.id.layout_total)
        FrameLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onSuccess(String id, String name);
    }
}
